﻿#include <tchar.h>
#include "Protocol.h"

/**
 * 创建添加好友请求。
 * @param dstUsername   目标用户名
 * @param srcIconIndex  源用户头像索引
 * @param srcUsername   源用户名
 * @return 添加好友请求
 */
AddFriendRequest* New_AddFriendRequest(LPCTSTR dstUsername, int srcIconIndex, LPCTSTR srcUsername) {
    AddFriendRequest* request = (AddFriendRequest*)calloc(1, sizeof(AddFriendRequest));
    _tcscpy_s(request->mDstUsername, PASSWORD_BUF_LEN, dstUsername);
    request->mSrcIconIndex = srcIconIndex;
    _tcscpy_s(request->mSrcUsername, USERNAME_BUF_LEN, srcUsername);
    request->mType = ADD_FRIEND_REQUEST;

    return request;
}

/**
 * 创建添加好友结果。
 * @param dstIconIndex  目标用户头像索引
 * @param dstUsername   目标用户名
 * @param resultCode    结果码
 * @param srcUsername   源用户名
 * @return 添加好友结果
 */
AddFriendResult* New_AddFriendResult(int dstIconIndex, LPCTSTR dstUsername, ResultCode resultCode, LPCTSTR srcUsername) {
    AddFriendResult* result = (AddFriendResult*)calloc(1, sizeof(AddFriendResult));
    result->mDstIconIndex = dstIconIndex;
    _tcscpy_s(result->mDstUsername, USERNAME_BUF_LEN, dstUsername);
    result->mResultCode = resultCode;
    if (srcUsername != NULL) {
        _tcscpy_s(result->mSrcUsername, USERNAME_BUF_LEN, srcUsername);
    }
    result->mType = ADD_FRIEND_RESULT;
    return result;
}

/**
 * 创建聊天信息。
 * @param content     内容
 * @param dstUsername 目标用户名
 * @param srcUsername 源用户名
 * @param time        生成时间
 * @return 聊天信息
 */
ChatInfo* New_ChatInfo(LPCTSTR content, LPCTSTR dstUsername, LPCTSTR srcUsername, LPCTSTR time) {
    ChatInfo* chatInfo = (ChatInfo*)calloc(1, sizeof(ChatInfo));
    _tcscpy_s(chatInfo->mContent, CONTENT_BUF_LEN, content);
    _tcscpy_s(chatInfo->mDstUsername, USERNAME_BUF_LEN, dstUsername);
    _tcscpy_s(chatInfo->mSrcUsername, USERNAME_BUF_LEN, srcUsername);
    _tcscpy_s(chatInfo->mTime, DATE_TIME_BUF_LEN, time);
    chatInfo->mType = CHAT;
    return chatInfo;
}

/**
 * 创建好友登录通知。
 * @param username  用户名
 * @return 好友登录通知
 */
FriendLogon* New_FriendLogon(LPCTSTR username) {
    FriendLogon* friendLogon = (FriendLogon*)calloc(1, sizeof(FriendLogon));
    friendLogon->mType = FRIEND_LOGON;
    _tcscpy_s(friendLogon->mUsername, USERNAME_BUF_LEN, username);
    return friendLogon;
}

/**
 * 创建离线请求。
 * @param username  用户名
 * @return 离线请求
 */
LogoffRequest* New_LogoffRequest(LPCTSTR username) {
    LogoffRequest* request = (LogoffRequest*)calloc(1, sizeof(LogoffRequest));
    request->mType = LOGOFF;
    _tcscpy_s(request->mUsername, USERNAME_BUF_LEN, username);
    return request;
}

/**
 * 创建登录请求。
 * @param password  密码
 * @param username  用户名
 * @return 登录请求
 */
LogonRequest* New_LogonRequest(LPCTSTR password, LPCTSTR username) {
    LogonRequest* request = (LogonRequest*)calloc(1, sizeof(LogonRequest));
    request->mType = LOGON;
    _tcscpy_s(request->mUserInfo.mPassword, PASSWORD_BUF_LEN, password);
    _tcscpy_s(request->mUserInfo.mUsername, USERNAME_BUF_LEN, username);
    return request;
}

/**
 * 创建登录结果。
 * @return 登录结果
 */
LogonResult* New_LogonResult() {
    LogonResult* result = (LogonResult*)calloc(1, sizeof(LogonResult));
    result->mIconIndex = -1;
    result->mResultCode = SUCCEEDED;
    result->mType = LOGON;
    return result;
}

/**
 * 创建注册请求。
 * @param iconIndex 头像索引
 * @param password  密码
 * @param username  用户名
 * @return 注册请求
 */
RegRequest* New_RegRequest(int iconIndex, LPCTSTR password, LPCTSTR username) {
    RegRequest* request = (RegRequest*)calloc(1, sizeof(RegRequest));
    request->mType = REG;
    _tcscpy_s(request->mUserInfo.mUsername, USERNAME_BUF_LEN, username);
    _tcscpy_s(request->mUserInfo.mPassword, PASSWORD_BUF_LEN, password);
    request->mUserInfo.mIconIndex = iconIndex;
    return request;
}

/**
 * 创建注册结果。
 * @return 注册结果
 */
RegResult* New_RegResult() {
    RegResult* result = (RegResult*)calloc(1, sizeof(RegResult));
    result->mResultCode = SUCCEEDED;
    result->mType = REG;
    return result;
}
